<?php

namespace App\Clients\Keruyun;

use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Database\Eloquent\Model;

class IncomeListByPage extends KeruyunClient
{
    public function __construct(Model $account){
        parent::__construct($account);
//        var_dump(123);
//        exit();
        $this->queryData = [
 //           "ccc"=>111
            'group' => 'HSF',
            '@class' => 'com.alsc.saas.gw.client.HsfReq',
            'service' => 'com.alsc.saas.data.reportcenter.sdk.client.gw.frontend.order.businessincome.v2.BusinessIncomeStatisticalV3Client',
            'method' => 'listByPage',
            'version' => '1.0.0',
            'eagleEyeUserData' => new \stdClass(),
            'argTypes' => [
                'com.alsc.saas.data.reportcenter.sdk.client.gw.model.businessincome.v3.request.BusinessIncomeV3Req'
            ],
            'argValues' => [
                [
                    'couponStatisticalType' => 'BY_NAME',
                    'storeStatisticalType' => 'SEPARATE',
                    'orgStatisticsType' => 'BY_SHOP',
                    'shopIds' => [
                        '40282015'
                    ],
                    'dateRange' => [
                        'dateType' => 'FINISH_BUSI_DATE',
                        'startDate' => 1755273600000,
                        'endDate' => 1755359999999
                    ],
                    'periodType' => 'BY_DAY',
                    'orderSourceList' => [],
                    'orderTypeList' => [],
                    'pageBean' => [
                        'pageNum' => 1,
                        'pageSize' => 100,
                        'collators' => [
                            [
                                'field' => 'busiDate',
                                'desc' => true
                            ]
                        ]
                    ],
                    'extInfos' => [
                        'pageUri' => '/lowcode/business-income',
                        'startDate' => 1755273600000,
                        'endDate' => 1755359999999
                    ]
                ]
            ],
            'targetClusterName' => 'alibaba-prod',
            'timeout' => 'PT30S',
            'context' => [
                'visitedDevice' => 'WEB'
            ]
        ];
//        var_dump(json_encode($this->queryData));
//        exit();
    }

    public function handleQueryData($queryData)
    {
        $this->queryData["argValues"][0]["shopIds"] = [$queryData["merchantNo"]];
        $this->queryData["argValues"][0]["dateRange"]["startDate"] = $queryData["startDate"];
        $this->queryData["argValues"][0]["extInfos"]["startDate"] = $queryData["startDate"];
        $this->queryData["argValues"][0]["dateRange"]["endDate"] = $queryData["endDate"];
        $this->queryData["argValues"][0]["extInfos"]["endDate"] = $queryData["endDate"];
    }


    /**
     * @throws GuzzleException
     */
    public function execute($queryData = []): IncomeListByPage
    {
//        var_dump(json_encode($queryData));
//        exit();
        return $this->post("https://saas.keruyun.com/saas-gw/secure/v2/req?hsfApi=listByPage",$queryData);
    }
}
